package com.wobito.common.config;

import com.wobito.common.constants.BaseResponse;
import com.wobito.common.constants.BusinessException;
import com.wobito.common.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @Author Wangbaba
 * @Date 2024/4/25 16:08
 * @Version 1.0
 * 全局异常处理类
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e) {
        log.error("BusinessException: " + e.getMessage(), e);
        return ResultUtils.error(e.getCode(), e.getMessage(), e.getDescription());
    }

    @ExceptionHandler(BindException.class)
    public BaseResponse businessExceptionHandler(BindException ex) {
        log.error("BindException: " + ex.getMessage(), ex);
        String msg = ex.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        return ResultUtils.error(40043, msg,msg);
    }


}
